package com.dxb.security.controller;

import com.dxb.security.entity.Msg;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by sang on 2017/1/10.
 */
@Controller
public class HomeController {
    @RequestMapping("/")
    public String index(Model model) {
        Object principl = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String currentAuth = "";
        if (principl instanceof UserDetails) {
            currentAuth = ((UserDetails) principl).getAuthorities().stream().map(role -> ((GrantedAuthority) role).getAuthority()).reduce((r1, r2) -> r1 + r2).get();

        }
        Msg msg = new Msg("测试标题", "测试内容" + currentAuth, "额外信息，只对管理员显示");
        model.addAttribute("msg", msg);
        return "index";
    }
}
